﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using SL.MVVMS.RIA;
using MVVM_S.Samples.HelpDesk.Web.Models;
using MVVM_S.Samples.HelpDesk.Services;
using System.Collections.Specialized;

namespace MVVM_S.Samples.HelpDesk.ViewModels
{
    /// <summary>
    /// This is a basic ViewModel Template using the MVVM-S Toolkit
    /// ViewModels inherit from the ViewModel base class, and implement
    /// service classes as needed, which provide data to the View Model
    /// You Should be able to assume that operations in the ViewModel 
    /// all occur on the UI thread, since the ServiceModel deals with
    /// threading.  Also, data should be blendable, if your service 
    /// implements a design data template as well.
    /// 
    /// Use the <see cref="slvmprop">slvmprop</see> snippet to create Properties in the ViewModel
    /// Use the <see cref="slvmcollection">slvmcollection</see> snippet to expose Collections
    /// Use the <see cref="slvmcmd">slvmcmd</see> snippet to create Commands in the ViewModel
    /// </summary>
    public class OpenTicketViewModel : ViewModel
    {
        HelpdeskService dataService;
        public OpenTicketViewModel()
        {
            dataService = new HelpdeskService();

            // DONE: Add setupCommand and setupCollection functions here
            // to construct those items as needed
            setupOpenTicketsCollection();
            loadOpenTicketsCollection();
            setupNotifications();
        }

        /// <summary>
        /// Add this to your Constructor in your View Model
        /// </summary>
        void setupNotifications()
        {
            SL.MVVMS.RIA.Notifications.ReceiveNotification +=
                new WeakEventHandler<NotificationEventArg>(notification_Received).Handler;
        }

        /// <summary>
        /// Process all your Notifications Here
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void notification_Received(object sender, NotificationEventArg args)
        {
            // Note: This must be public to allow the WeakEventHandler
            // to provide the delegate

            // DONE: Process Messages Recieved Here
            if (args.Message == "Lookups Refreshed")
            {
                OpenTickets.Refresh();
            }
        }


        // DONE: Use the snippets slvmprop, slvmcollection, and slvmcmd to create 
        // Properties, Collections, and Commands for your View to bind to

       
        #region OpenTickets - Collection of Ticket
        /// <summary>
        /// Bind to this property in your View
        /// </summary>
        public PagedCollectionView OpenTickets { get; private set; }

        /// <summary>
        /// Call this from your constructor to create the View on load
        /// </summary>
        void setupOpenTicketsCollection()
        {
            OpenTickets = new PagedCollectionView(dataService.Tickets);
            using (OpenTickets.DeferRefresh())
            {
                // Use these examples to get started with Sorting, Grouping and Filtering
                OpenTickets.SortDescriptions.Add(      // Apply Sorting
                   new SortDescription("Updated", ListSortDirection.Descending));
                // OpenTickets.GroupDescriptions.Add(     // Apply Grouping
                //    new PropertyGroupDescription("PropertyName"));
                // OpenTickets.Filter += (obj) =>    // Apply a filter
                //    {
                //        Ticket item = obj as Ticket;
                //        if (item == null)
                //            return false;
                //
                //        // Apply your Filtering Logic Here by inspecting the Ticket item.
                //        if (item.PropertyName)
                //            return true;
                //        else
                //            return false;
                //    };
            }
        }

        /// <summary>
        /// Call this to Load and Refresh the OpenTickets of Ticket data afterwards
        /// </summary>
        void loadOpenTicketsCollection()
        {
            dataService.LoadOpenTickets(() =>
                {
                    OpenTickets.Refresh();
                });
        }
        #endregion
                
    }
}
